<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>函数防抖和节流</title>
    <style type="text/css">

    </style>
</head>
<body>
<p class="container"></p>
<script>

    var num = 1;
    var content = document.querySelector('.container');

    function addPlus() {
        num++;
        content.innerHTML = num
    }

    document.onmousemove = debounceIm(addPlus,1000,true)

    // 一般的防抖函数
    function debounceIm(doSomething,wait,isImmediate){
        var timeout;
        return function(){
            var _this = this,
                _arguments = arguments;
            clearTimeout(timeout);
            if(isImmediate){

                var isTrigger = !timeout;
                console.log(timeout);
                timeout = setTimeout(function(){
                    timeout = null;

                }, wait)
                isTrigger&&doSomething.apply(_this,_arguments);
            }else{
                timeout = setTimeout(function(){
                    doSomething.apply(_this,_arguments);
                },wait);
            }
        }
    }




</script>
</body>
</html>
